function Add(){
    location.href ='/Book/AddorEndit';
}
function Endit(id){
    location.href = `/Book/AddorEndit/${id}`;
}

function option(){
    let options = document.getElementsByClassName('option');
    for (let i = 0; i < options.length; i++) {
        if(options[i].selected){
           return options[i].value;
        }
        
    }
}
function Save(){
    let Id = $('#Id').val();
    let BookName = $('#BookName').val()
    let LibraryId;

    if(Id==''){
        LibraryId = option()?option():1;

    }else{
        LibraryId = option()?option():$('#LibraryId').val()
    }
    let obj ={
        Id,
        BookName,
        LibraryId
    }
    console.log(obj);
    // console.log($('#LibraryId').val());
    // console.log(document.getElementById('LibraryId').value);
    // console.log(document.getElementById('LibraryId').innerText);
    $.post('/Book/Save',obj,function(){
        location.href = '/Book/Index';
    })

}

function Delete(id){
    let msg = '确定删除吗？';
    if(confirm(msg)){
       location.href = `/Book/Delete/${id}`; 
    } 
}

function Cancal(){
    location.href = '/Book/Index';
}

Select = 1;
function SelectChange(){
    let options = document.getElementsByClassName('option');
    let find = document.getElementById('find');
    for (let k = 0; k < options.length; k++) {
        if(options[k].selected){
            if(options[k].value=="1"){
                find.placeholder = "请输入书名";
                Select=1;
            }else if(options[k].value=="2"){
                find.placeholder = "请输入Id";
                Select=2;
            }else{
                find.placeholder = "请输入图书馆Id";
                Select=3;
            }
        }
        
    }
}

function Find(){
    let Str = $('#find').val();
    if(Select!=1&&isNaN(Number(Str))){
        alert('请输入纯数字');
        location.href='/Book/Index';
    }else if(Str.length==0){
        alert('查找条件不允许为空');
        location.href='/Book/Index';
    }
    else{
        let obj = {
            Str,
            Select
        }
        console.log(obj);

        $.post('/Book/Select',obj,function(x){
            console.log(x);
            $('.row').html('');
            x.forEach(item => {
                let html = 
                `
                <tr class="row">
                <td>${item.id}</td>
                <td>${item.bookName}</td>
                <td>${item.createtime}</td>
                <td>${item.libraryId}</td>
                <td>
                    <input type="button" value="编辑" onclick="Endit(${item.id})">
                    <input type="button" value="删除" onclick="Delete(${item.id})">
                </td>
            </tr>  
            `
            $('table').append(html);
            });
            document.getElementById('back').style.display='inline-block';
        })
    }
}

function Back(){
    location.href = '/Book/Index';
}

$(function (){
    let rows = document.getElementsByClassName('row');
    console.log(rows);
    for (let m = 0; m < rows.length; m++) {
        if(m%2==0){
            rows[m].style.background = 'rgb(200,225,230)';
        }else
        {
            rows[m].style.background = 'rgb(170,200,200)';
        }
        
    }
})